//
//  HomeVC.swift
//  DLuxTravel
//
//  Created by 陆涛 on 2018/11/21.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class HomeVC: BaseVC {

    @IBOutlet weak var tableView: BaseTableView!
    
    private let headHeight = kScreenWidth * 9 / 16
    private var headView: PagerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    private func setupUI() {
        
        setupHeadView()
        
        self.closeAutomaticallyInsets(scrollview: tableView)
        self.setNavBarStyle(.transparency)
        
        tableView.register(UINib(nibName: "TopRateCell", bundle: nil), forCellReuseIdentifier: "TopRateCell")
        tableView.register(UINib(nibName: "AllHotelCell", bundle: nil), forCellReuseIdentifier: "AllHotelCell")
        
        tableView.register(SectionHeaderView.self, forHeaderFooterViewReuseIdentifier: "SectionHeaderView")
        
        self.addLeftItem(icon: UIImage(named: "search")) {
            
            [weak self] in
            guard let `self` = self else { return }
            
            let searchVC = SearchVC()
            
            let nav = BaseNav(rootViewController: searchVC)
            
            self.present(nav, animated: true, completion: nil)
        }
        
        self.addRightItem(icon: UIImage(named: "我的")) {
            
            AppManager.share.loginPage()
        }
    }
    
    private func setupHeadView() {
 
        var picArray:[Any] = []
        
        for i in 1..<6 {
            
            if let image = UIImage(named: "首页\(i).jpg") {
                
                picArray.append(image)
            }
        }
        
        let frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: headHeight)
        headView = PagerView(frame: frame)
        
        headView.pics = picArray
        
        tableView.tableHeaderView = headView
        tableView.estimatedRowHeight = 100
    }
    
    private func hotelDetailAction() {
        
        let detailVC = HotelDetailVC()
        
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
}


extension HomeVC: UITableViewDataSource, UITableViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offsetY = scrollView.contentOffset.y
        
        self.headView.setFactor(offset: offsetY)
        
        if offsetY > 0 {
            
            let alpha = offsetY / CGFloat(headHeight - kNavigationBarHeight)
            
            self.setNavBarStyle(.blackAlpha(alpha))
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            return 1
        }
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "TopRateCell") as! TopRateCell
            
            cell.hotelDetailAction = {
                
                [weak self] in
                guard let `self` = self else { return }
                
                self.hotelDetailAction()
            }
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "AllHotelCell") as! AllHotelCell
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 0 {
            
            return TopRateCell.cellHeight()
        }
        
        return AllHotelCell.cellHeight()
    }
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeaderView") as! SectionHeaderView
        view.title = "Top-rated Hotel"
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 64
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        hotelDetailAction()
    }
}
